package org.pulloid.android;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.pulloid.Cursor;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

public class PulloidAdapter extends BaseAdapter {
	
	Cursor<Record> cursor;
	List<Record> elements = new ArrayList<Record>();
	boolean endOfCursor = false;
	int resource;
	int dropDownResource;
	LayoutInflater inflater;
	
	public PulloidAdapter(Context context, int resource) {
        this.resource = resource;
        this.dropDownResource = resource;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
	
	public AndroidRootContext createRootContext() {
		return new AndroidRootContext(this);
	}

	@Override
	public int getCount() {
		populateUpToPosition(Integer.MAX_VALUE);
		return elements.size();
	}

	@Override
	public Record getItem(int position) {
		populateUpToPosition(position);
		if(elements.size() > position)
			return elements.get(position);
		return null;
	}

	private void populateUpToPosition(int pos) {
		while(elements.size() <= pos) {
			if(cursor.hasNext()) {
				elements.add(cursor.next());
			} else {
				endOfCursor = true;
				break;
			}
		}
	}

	@Override
	public long getItemId(int position) {
		Record rec = getItem(position);
		if(rec != null)
			return rec.getId();
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            v = this.inflater.inflate(this.resource, parent, false);
        } else {
            v = convertView;
        }
        bindView(position, v);
        return v;
    }
	

    
	private void bindView(int position, View view) {
		final Record rec = getItem(position);
		if(rec == null)
			return;
		
        for(Map.Entry<String, Object> field : rec.entrySet()) {
            final View v = view.findViewById(Integer.parseInt(field.getKey())); // Key is supposed to be a resource ID in string format
            if (v != null) {
                final Object data = field.getValue();
                String text = (data == null ? "" : data.toString());

                if(v instanceof Checkable) {
                    if(data instanceof Boolean)
                        ((Checkable)v).setChecked((Boolean) data);
                    else if(data instanceof Number)
                        ((Checkable)v).setChecked(((Number)data).doubleValue() >= 1);
                    else
                        ((Checkable)v).setChecked("true".equalsIgnoreCase(text) || "1".equals(text));
                } else if (v instanceof ImageView && data instanceof Integer) {
                        ((ImageView)v).setImageResource((Integer)data);
                } else if(v instanceof TextView) {
                	if(data instanceof Integer)
                		((TextView)v).setText((Integer)data);
                	else
                		((TextView)v).setText(text);
                }
            }
        }
    }
}
